#include"rle.h"
#include<iostream>
#include<vector>

using namespace rle;
using namespace std;


RLE::RLE():Step(){

}
RLE::~RLE(){

}
/* cnt must be less than 255,'cause cnt just one byte*/
void
RLE::encode(uint8_t* in_buf,uint8_t* out_buf,uint32_t buf_size){
}

void
RLE::decode(uint8_t* in_buf,uint8_t* out_buf,uint32_t buf_size){
}

void RLE::encode(hzstream* hz){
  uint8_t* pInBuf=reinterpret_cast<uint8_t*>(hz->in_buf);
  vector<uint8_t> vec_buf;
  for(;pInBuf<(hz->in_buf + hz->in_size);pInBuf++){
    if(*pInBuf ==0){
       uint32_t cnt=0;
       while(*pInBuf == 0 && cnt< 255){
          cnt++;  
          pInBuf++;
       };
       pInBuf--;
       if(cnt !=1){
         vec_buf.push_back((uint8_t)7);
         vec_buf.push_back((uint8_t)cnt);
      }else{
         vec_buf.push_back(*pInBuf);
      }
    }else{
          vec_buf.push_back(*pInBuf);
    }
  }
  hz->out_size = vec_buf.size();
  hz->out_buf = new uint8_t[hz->out_size];
  for(uint8_t i=0;i < hz->out_size;i++){
    hz->out_buf[i] = vec_buf.at(i);
  }
}

void RLE::decode(hzstream* hz){
 uint8_t* pInBuf=hz->in_buf;
 vector<uint8_t> vec_buf;
	for(;pInBuf<(hz->in_buf + hz->in_size);pInBuf++){
		if(*pInBuf != (uint8_t)7){
			vec_buf.push_back(*pInBuf);
		}else{
			uint32_t cnt =1;
			cnt=(uint32_t)*(pInBuf+1);
			while(cnt--)vec_buf.push_back((uint8_t)0);
				pInBuf++;
		}
	}
	hz->out_size = vec_buf.size();
	hz->out_buf = new uint8_t[hz->out_size];
	for(uint8_t i=0;i < hz->out_size;i++){
		hz->out_buf[i] = vec_buf.at(i);
	}
}
